﻿/*
Copyright (c) 2010, hiSoft
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the hiSoft nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE HISOFT AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL HISOFT BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

using System;
using System.IO;
using System.Threading;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Orchard.Test.Library;
using Selenium;
using RecordPlaybackFramework;


namespace Orchard.Test.Modules.Media.Auto
{
    [TestClass]
    public class CheckToLockAspectRatioWhenEditMedia
    {
        private TestLibrary TestLibrary;
        private ISelenium selenium;

        [TestInitialize]
        public void SetupTest()
        {
            TestLibrary = TestLibrary.SetupTest(this.ToString());
            selenium = TestLibrary.Selenium;
        }

        [TestCleanup]
        public void TeardownTest()
        {
            TestLibrary.ShutDown();
        }
        
        [TestMethod]
        public void Media_CheckToLockAspectRatioWhenEditMedia()
        {
            TestLibrary.UserHelper.LogOnAsAdmin();
            var post = TestLibrary.DefaultBlog.CreateRandomPost();
            MediaProperties mediaProp=new MediaProperties();
            mediaProp.LockAspectRatio=true;
            mediaProp.Width="100";    //original size :435px × 446px , if width:100,then as ratio, height will be 103 px
            TestLibrary.MediaHelp.InsertMediaFromUrlNotSubmit(TestLibrary.Consts.StrExamplePictureUrl, mediaProp);  //image1
             
            MediaProperties mediaProp1 = new MediaProperties();
            mediaProp1.LockAspectRatio = false;  //as default,according to ratio zoom the picture
            mediaProp1.Width = "100";
            TestLibrary.MediaHelp.InsertMediaFromUrlNotSubmit(TestLibrary.Consts.StrExamplePictureUrl, mediaProp1); //image2
            
            MediaProperties mediaProp2 = new MediaProperties();
            mediaProp2.LockAspectRatio = true;
            mediaProp2.Height = "103";
            TestLibrary.MediaHelp.InsertMediaFromUrlNotSubmit(TestLibrary.Consts.StrExamplePictureUrl, mediaProp2);  //image3

            MediaProperties mediaProp21 = new MediaProperties();
            mediaProp21.LockAspectRatio = false;
            mediaProp21.Height = "103";
            TestLibrary.MediaHelp.InsertMediaFromUrlNotSubmit(TestLibrary.Consts.StrExamplePictureUrl, mediaProp21); //image4


            MediaProperties mediaProp3 = new MediaProperties();
            mediaProp3.LockAspectRatio = true;
            mediaProp3.Width = "100";
            mediaProp3.Height = "50";   //because of lock ratio, the picture will be insert as size: 49px*50px
            TestLibrary.MediaHelp.InsertMediaFromUrlNotSubmit(TestLibrary.Consts.StrExamplePictureUrl, mediaProp3);//image5
            
            MediaProperties mediaProp4 = new MediaProperties();
            mediaProp4.LockAspectRatio = false;
            mediaProp4.Width = "100";
            mediaProp4.Height = "50";
            TestLibrary.MediaHelp.InsertMediaFromUrlNotSubmit(TestLibrary.Consts.StrExamplePictureUrl, mediaProp4);//image6

            selenium.Click("submit.Publish"); //submit
            if(!selenium.GetLocation().EndsWith("/default-test-blog/"+post.Permalink))
                selenium.Open("/default-test-blog/"+post.Permalink);
            string locatorImageInViewPost = "//div[@id='content']/div/div/article/p/img";
            Assert.IsTrue(selenium.GetXpathCount(locatorImageInViewPost) == 6);
            string widthOfPicture1 = selenium.GetElementWidth(locatorImageInViewPost+"[1]").ToString();
            string heightOfPicture1 = selenium.GetElementHeight(locatorImageInViewPost+"[1]").ToString();

            string widthOfPicture2 = selenium.GetElementWidth(locatorImageInViewPost+"[2]").ToString();
            string heightOfPicture2 = selenium.GetElementHeight(locatorImageInViewPost+"[2]").ToString();

            string widthOfPicture3 = selenium.GetElementWidth(locatorImageInViewPost + "[3]").ToString();
            string heightOfPicture3 = selenium.GetElementHeight(locatorImageInViewPost + "[3]").ToString();

            string widthOfPicture4 = selenium.GetElementWidth(locatorImageInViewPost + "[4]").ToString();
            string heightOfPicture4 = selenium.GetElementHeight(locatorImageInViewPost + "[4]").ToString();

            string widthOfPicture5 = selenium.GetElementWidth(locatorImageInViewPost + "[5]").ToString();
            string heightOfPicture5 = selenium.GetElementHeight(locatorImageInViewPost + "[5]").ToString();

            string widthOfPicture6 = selenium.GetElementWidth(locatorImageInViewPost + "[6]").ToString();
            string heightOfPicture6 = selenium.GetElementHeight(locatorImageInViewPost + "[6]").ToString();

            Assert.IsTrue(widthOfPicture1 == widthOfPicture2 && widthOfPicture1 == widthOfPicture3 && widthOfPicture1 == widthOfPicture4);
            Assert.IsTrue(heightOfPicture1 == heightOfPicture2 && heightOfPicture1 == heightOfPicture3 && heightOfPicture1 == heightOfPicture4);
            Assert.IsTrue(widthOfPicture5=="49" && heightOfPicture5=="50");
            Assert.IsTrue(widthOfPicture6 == "100" && heightOfPicture6 == "50");
        }
    }
}
